//
//  WBVisitorView.swift
//  WBWeiBo
//
//  Created by 王斌 on 16/7/9.
//  Copyright © 2016年 王斌. All rights reserved.
//

import UIKit
import SnapKit

class WBVisitorView: UIView {
    
    //  定义登录或者注册执行的闭包
    var loginClosure:(()->())?
    
    //  MARK:  -- 懒加载控件
    //  旋转视图
    private lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //  罩层
    private lazy var maskIconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //  中心房子图片
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //  MARK: -- 注册按钮
    private lazy var registerButton:UIButton = {
        
       let button = UIButton()
       button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //  设置文本及文本颜色
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return button
    }()
    
    
    // MARK: --  登录按钮
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //  设置文本及文本颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)

        return button
    }()
    
    
    //  MARK: -- 消息内容
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜 关注一些事,回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        
        //  多行显示
        label.numberOfLines = 0
        
        //  居中显示
        label.textAlignment = .Center
        
        return label
    }()
    
    
    //  使用手写代码的方式去调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    
    
     //  使用xib或者storyboard会调用这个,没有实现xib,会给你提示
    required init?(coder aDecoder: NSCoder) {
        //支持xib写法
        super.init(coder: aDecoder)
        
        backgroundColor = UIColor.yellowColor()
        
        setupUI()
        
        
    }
    
    
    //  MARK: -- 添加视图,设置约束
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //添加控件
        addSubview(cycleImageView)      //  旋转视图
        addSubview(maskIconImageView)   //  罩层
        addSubview(iconImageView)       //  中心房子图片
        addSubview(messageLabel)        //  文本
        addSubview(registerButton)      //  注册
        addSubview(loginButton)         //  登录
        
        
        //设置约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
           
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
    }
    
    
    
    //MARK: --   开启旋转动画
    private func startAnimation(){
        
        let animationc = CABasicAnimation(keyPath: "transform.rotation")
        
        //  动画目的地
        animationc.toValue  = 2 * M_PI
        
        //  动画时长
        animationc.duration = 20
        
        //  动画的重复次数
        animationc.repeatCount = MAXFLOAT
        
        //  失去焦点 不释放资源
        animationc.removedOnCompletion = false
        
        //  执行动画
        cycleImageView.layer.addAnimation(animationc, forKey: nil)
    }
    
    
    // MARK: --  修改访客视图信息
    func updateVisitorInfo(message:String?,imageName:String?){
        
        //不是首页(消息,发现,我的)
        if let msg = message, imageName = imageName{
            messageLabel.text  = msg
            iconImageView.image = UIImage(named: imageName)
            
            //  隐藏旋转视图
            cycleImageView.hidden = true
        }else{
            //是首页
            //显示旋转视图
            cycleImageView.hidden = false
            
            // 执行旋转动画
            startAnimation()
        }
    }
    
    // MARK: --点击事件  
    @objc private func registerAction(){
        print("点击了注册")
        
        loginClosure?()
    }
    @objc private func loginAction(){
        
        print("点击了登录")
        loginClosure?()
    }
    

}
